﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.Cost
{
    public class Conditions
    {
        public Conditions(List<Condition> ConditionList)
        {
            this.m_ConditionList = ConditionList;
        }
        private Conditions()
        {
        }

        private List<Condition> m_ConditionList;


        public Condition Add()
        {
            Condition con = new Condition();
            m_ConditionList.Add(con);
            return con;
        }

        internal string GetSQLWhere()
        {
            string sql_where = string.Empty;
            string sql_temp = string.Empty;
            string Single_Quote = "'";
            string Double_Quote = "''";
            //如果条件列表为空则返回""
            if (m_ConditionList == null)
            {
                return string.Empty;
            }
            if (m_ConditionList.Count == 0)
            {
                return string.Empty;
            }
            foreach (Condition item in m_ConditionList)
            {
                /*如果值为空则跳至下轮 [2012-2-23 WangPeng]*/
                if (string.IsNullOrEmpty(item.CondVal))
                {
                    continue;
                }
                sql_temp = "";
                if (item.BracketOpenNum)
                {
                    sql_temp += " ( ";
                }
                sql_temp += item.Alias;//field
                switch (item.Operation)//operation
                {
                    case ConditionOperation.co_EQUAL:
                        sql_temp += " = ";
                        break;
                    case ConditionOperation.co_LIKE:
                        sql_temp += " LIKE ";
                        break;
                    case ConditionOperation.co_GRATER_THAN:
                        sql_temp += " > ";
                        break;
                    case ConditionOperation.co_LESS_THAN:
                        sql_temp += " < ";
                        break;
                    case ConditionOperation.co_GRATER_EQUAL:
                        sql_temp += " >= ";
                        break;
                    case ConditionOperation.co_LESS_EQUAL:
                        sql_temp += " <= ";
                        break;
                    case ConditionOperation.co_Not_EQUAL:
                        sql_temp += " <> ";
                        break;
                    default:
                        break;
                }
                if (item.IsEqualOtherAilas)
                {
                    sql_temp +=  item.CondVal ;//Data Field
                }
                else
                {
                    /*替换值中的单引号*/
                    item.CondVal = item.CondVal.Replace(Single_Quote, Double_Quote);
                    //Like字段值中添加%【2011-3-3 wp】
                    if (item.Operation == ConditionOperation.co_LIKE)
                    {
                        sql_temp += "N'%" + item.CondVal + "%'";//value
                    }
                    else
                    {
                        sql_temp += "N'" + item.CondVal + "'";//value
                    }
                }
                if (item.BracketCloseNum)
                {
                    sql_temp += " ) ";
                }
                switch (item.Relationship)
                {
                    case ConditionRelationship.cr_NONE:

                        break;
                    case ConditionRelationship.cr_AND:
                        sql_temp += " AND ";
                        break;
                    case ConditionRelationship.cr_OR:
                        sql_temp += " OR ";
                        break;
                    default:
                        break;
                }

                sql_where += sql_temp;
            }
            /*移除句尾条件  AND OR*/
            if (sql_where.EndsWith(" AND ") || sql_where.EndsWith(" OR "))
            {
                sql_where = sql_where.Substring(0, sql_where.Length - 4);
            }

            return sql_where;
        }

        internal string GetSQLWhere(string Name)
        {
            string sql_where = string.Empty;
            string sql_temp = string.Empty;
            //如果条件列表为空则返回""
            if (m_ConditionList == null)
            {
                return string.Empty;
            }
            if (m_ConditionList.Count == 0)
            {
                return string.Empty;
            }
            foreach (Condition item in m_ConditionList)
            {
                sql_temp = "";
                if (item.BracketOpenNum)
                {
                    sql_temp += " ( ";
                }
                sql_temp += Name + item.Alias;//field
                switch (item.Operation)//operation
                {
                    case ConditionOperation.co_EQUAL:
                        sql_temp += " = ";
                        break;
                    case ConditionOperation.co_LIKE:
                        sql_temp += " LIKE ";
                        break;
                    case ConditionOperation.co_GRATER_THAN:
                        sql_temp += " > ";
                        break;
                    case ConditionOperation.co_LESS_THAN:
                        sql_temp += " < ";
                        break;
                    case ConditionOperation.co_GRATER_EQUAL:
                        sql_temp += " >= ";
                        break;
                    case ConditionOperation.co_LESS_EQUAL:
                        sql_temp += " <= ";
                        break;
                    case ConditionOperation.co_Not_EQUAL:
                        sql_temp += " <> ";
                        break;
                    default:
                        break;
                }
                //Like字段值中添加%【2011-3-3 wp】
                if (item.Operation == ConditionOperation.co_LIKE)
                {
                    sql_temp += "N'%" + item.CondVal + "%'";//value
                }
                else
                {
                    sql_temp += "N'" + item.CondVal + "'";//value
                }
                if (item.BracketCloseNum)
                {
                    sql_temp += " ) ";
                }
                switch (item.Relationship)
                {
                    case ConditionRelationship.cr_NONE:

                        break;
                    case ConditionRelationship.cr_AND:
                        sql_temp += " AND ";
                        break;
                    case ConditionRelationship.cr_OR:
                        sql_temp += " OR ";
                        break;
                    default:
                        break;
                }

                sql_where += sql_temp;
            }
            //添加TOP条件

            return sql_where;
        }
    }
}
